package com.example.demo.controller;

import com.example.demo.dao.entity.Department;
import com.example.demo.dao.entity.Doctor;
import com.example.demo.dao.entity.Hospital;
import com.example.demo.service.DepartmentService;
import com.example.demo.service.DoctorService;
import com.example.demo.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DoctorService doctorService;

    /**
     * 查询科室列表
     * @param model
     * @param department
     * @return
     */
    @GetMapping("/list")
    public String getDepartmentList(Model model, Department department) {
        List<Department> list = departmentService.lambdaQuery().eq(Department::getStatus,0).list();
        list.forEach(item -> {
            item.setHospitalName(hospitalService.getById(item.getHospitalId()).getHospitalName());
        });
        model.addAttribute("list", list);
        return "department/list";
    }

    /**
     * 新增科室页面
     * @param model
     * @param department
     * @param id
     * @return
     */
    @GetMapping("addpage")
    public String addpage(Model model, @ModelAttribute Department department, @RequestParam(value = "id",required = false) Integer id){
        if(!StringUtils.isEmpty(id)){
            Department byId = departmentService.getById(id);
            model.addAttribute("department",byId);
            Hospital hospital = hospitalService.getById(byId.getHospitalId());
            model.addAttribute("hospital",hospital);
        }
        List<Hospital> list = hospitalService.list();
        model.addAttribute("hospitals",list);
        return "department/add";
    }

    /**
     * 保存更新科室
     * @param model
     * @param department
     * @param principal
     * @return
     */
    @PostMapping(value = {"/add", "/update"}, produces = "application/json")
    public String saveOrUpdate(Model model, Department department, Principal principal) {
        String loginName = principal.getName();
        Date now = new Date();
        department.setCreatePerson(loginName);
        department.setUpdatePerson(loginName);
        if(department.getId() == null){
            department.setCreateTime(now);
        }
        department.setUpdateTime(now);
        departmentService.saveOrUpdate(department);
        return getDepartmentList(model,department);
    }


    /**
     * 删除科室
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public String deleteDepartmentById( @PathVariable Long id) {
        departmentService.lambdaUpdate().eq(Department::getId,id).set(Department::getStatus,1).update();
        return "success";
    }

    @PostMapping("/getDepartment")
    @ResponseBody
    public List getDepartment(String hid){
        List<Department> list = departmentService.lambdaQuery().eq(Department::getStatus,0)
                .eq(Department::getHospitalId,hid).list();
        return list;
    }

    @PostMapping("/getDoctor")
    @ResponseBody
    public List getDoctor(String did){
        List<Doctor> list = doctorService.lambdaQuery().eq(Doctor::getStatus,0)
                .eq(Doctor::getDepartmentId,did).list();
        return list;
    }
}
